<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-02
 * Time: 16:04
 */

namespace app\src\product\logic;


use app\src\base\logic\BaseLogic;
use app\src\goods\logic\ProductMemberPriceLogic;
use app\src\goods\logic\ProductSkuLogic;
use app\src\powersystem\model\AuthGroupAccess;
use app\src\user\enum\RoleEnum;
use app\src\user\logic\MemberLogic;
use app\src\product\model\Product;
use app\src\product\model\ProductImage;
use app\src\product\logic\ProductImageLogic;
use think\Db;

class ProductLogic extends BaseLogic
{
    public function _init()
    {
        $this->setModel(new Product());
    }


    public function plist($map,$order='',$fields=''){

        $query = $this->getModel();
        if(!empty($map)) $query = $query->where($map);
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query
            //->join('itboye_product_image','itboye_product_image.pid=itboye_product.id AND itboye_product_image.type = 6015')
            -> select();
      //  dump($list);exit;
        if(!$list)  return $this -> apiReturnSuc($list);
        foreach($list as $k=>$v){
            //查询主图
            $img=(new ProductImageLogic())->getInfo(['pid'=>$v['id'],'type'=>6015]);//
            if(!$img['status']) $this->error('查询出错');
            $list[$k]['img_main']=$img['info'];
//            $img=(new ProductImageLogic())->queryNoPaging(['pid'=>$v['id'],'type'=>6016]);//
//            if(!$img['status']) $this->error('查询出错');
//            $list[$k]['img_follow']=$img['info'];
//            //查询sku
//            $sku=(new ProductSkuLogic())->getinfo(['product_id'=>$v['id']]);
//            if(!$sku['status']) $this->error('查询商品信息出错');
//            $list[$k]['sku']=$sku['info'];
            //查询分组信息
//
//            $classify=(new ProductMemberPriceLogic())->getInfo(['p_id'=>$v['id'],'member_group_id'=>$role]);
//
//            if(!$classify['status']) $this->error('查询商品信息出错');
//            $list[$k]['classify']=$classify['info'];
        }

        $data = [];
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }
        }
        return $this -> apiReturnSuc($data);
    }


    public function detail($map,$order='',$fields=''){

        $query = $this->getModel();
        if(!empty($map)) $query = $query->where($map);
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query
            //->join('itboye_product_image','itboye_product_image.pid=itboye_product.id AND itboye_product_image.type = 6015')
            -> select();
        if(!$list) $this->error('查询数据出错');
        foreach($list as $k=>$v){
            //查询主图
            $img=(new ProductImageLogic())->getInfo(['pid'=>$v['id'],'type'=>6015]);//
            if(!$img['status']) $this->error('查询出错');
            $list[$k]['img_main']=$img['info'];
            $img=(new ProductImageLogic())->queryNoPaging(['pid'=>$v['id'],'type'=>6016]);
            if(!$img['status']) $this->error('查询出错');
            $list[$k]['img_follow']=$img['info'];
//            //查询sku
//            $sku=(new ProductSkuLogic())->getinfo(['product_id'=>$v['id']]);
//            if(!$sku['status']) $this->error('查询商品信息出错');
//            $list[$k]['sku']=$sku['info'];
            //查询分组信息
//            $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$v['id']]);//,'member_group_id'=>$role
//
//            if(!$classify['status']) $this->error('查询商品信息出错');
//            $list[$k]['classify']=$classify['info'];
        }

        $data = [];
        foreach ($list as $vo){
            if(method_exists($vo,"toArray")){
                array_push($data,$vo->toArray());
            }
        }
        return $this -> apiReturnSuc($data);
    }


}